//
//  TextFiledView.m
//  Cxzc
//
//  Created by hwd on 17/8/7.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "TextFiledView.h"
@interface TextFiledView ()<UITextFieldDelegate>
{
    NSInteger limitNum;//限制字数
    NSArray *numberKeyboardArr;//使用数字键盘数组
    NSArray *englishKeyboardArr;//使用数字英文数组
    NSDictionary *limitDic;//限制的字数的字典
}
@end
@implementation TextFiledView

-(instancetype)initWithFrame:(CGRect)frame PlaceHodel:(NSString *)placehodel Title:(NSString *)title{
    if (self = [super initWithFrame:frame]) {//先加载父类
        [self.layer setCornerRadius:5*screenW_6];
        self.layer.masksToBounds = YES;
        [self addSubview:[self CreatUIPlaceHodel:placehodel Title:title]];
    }
    return self;
}
-(UITextField *)CreatUIPlaceHodel:(NSString *)placehodel Title:(NSString *)title
{
    if (!_inputTf) {//不存在 创建
        
        //Titlelab的大小控制
        NSInteger scrWidth = ([title isEqualToString:@"支付宝账号"] || [title isEqualToString:@"身份证号码"])?120*screenW_6:80*screenW_6;
        scrWidth = [title isEqualToString:@""]?0:scrWidth;
        //输入标题
        _nameLab = [TomamaUI createLabel:CGRectMake(20*screenW_6,0,scrWidth,50*screenW_6) textAligment:0 font:[UIFont systemFontOfSize:15*screenW_6] textColor:TextBlack text:title];
        [self addSubview:_nameLab];
        //输入框
        _inputTf = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_nameLab.frame),0,SCREENW-scrWidth,50*screenW_6)];
        _inputTf.font = [UIFont systemFontOfSize:15*screenW_6];
        _inputTf.placeholder = placehodel;
        _inputTf.delegate = self;
        _inputTf.returnKeyType =  UIReturnKeyDefault;
        [_inputTf setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
        _inputTf.textColor = [UIColor blackColor];
        [self addSubview:_inputTf];
        [_inputTf addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        //标题为空隐藏titleLab
//        if (![title isEqualToString:@""]) {
             [self addSubview:[ToolViewVC addCellLineLab:CGRectMake(15*screenW_6, 49*screenW_6, self.frame.size.width-30*screenW_6, 1*screenW_6)]];
//        }
        //键盘控制
        if ([self.numberKeyboardArr containsObject:placehodel]) {//数字键盘
            _inputTf.keyboardType = UIKeyboardTypeNumberPad;
        }else if ([self.englishKeyboardArr containsObject:placehodel]){//先英文键盘
            _inputTf.keyboardType = UIKeyboardTypeASCIICapable;
        }
        //字数限制
        if ([self.limitDic.allKeys containsObject:placehodel]) {
            limitNum = [[self.limitDic objectForKey:placehodel] integerValue];
        }else{//默认五十
            limitNum = 50;
        }
    }
    return _inputTf;
}

-(NSArray *)numberKeyboardArr//纯数字键盘
{
    if (!numberKeyboardArr) {//不存在构建数组
        numberKeyboardArr = @[@"请输入您的手机号码",
                              @"请输入您收到的验证码",
                              @"请输入手机号码",
                              @"请输入新手机号码",
                              @"请输入金额"];
    }
    return numberKeyboardArr;
}
-(NSArray *)englishKeyboardArr//先英文键盘
{
    if (!englishKeyboardArr) {//不存在构建数组
        englishKeyboardArr = @[@"请输入身份证号码",
                               @"请输入支付宝账号"];
    }
    return englishKeyboardArr;
}
-(NSDictionary *)limitDic//限制输入个数
{
    if (!limitDic) {//不存在构建字典
        limitDic = @{@"请输入昵称":@"16",
                     @"请输入姓名":@"16",
                     @"请输入手机号码":@"11",
                     @"请输入新手机号码":@"11",
                     @"请输入您的手机号码":@"11",
                     @"请输入您收到的验证码":@"10",
                     @"请输入身份证号码":@"22"};
    }
    return limitDic;
}
-(void)textFieldDidChange:(UITextField *)textField
{
    NSMutableString *toBeString = [NSMutableString stringWithString:textField.text];
    UITextRange *selectedRange = [textField markedTextRange];
    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
    if (!position) {//输入的实体字
        
        if (toBeString.length > limitNum && limitNum!=0)//不等于0且大于限制的长度
        {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:limitNum];
            if (rangeIndex.length == 1)//长度为1
            {
                textField.text = [toBeString substringToIndex:limitNum];
            }
            else//限制字数 去除多余
            {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, limitNum)];
                textField.text = [toBeString substringWithRange:rangeRange];
            }
        }else//回调
        {
            if (self.tfBlock) {//回调
                self.tfBlock(toBeString);
            }
            _inputTf.text = toBeString;
        }
    }
}

@end
